/*-----------------------------------------------------------------------------
	The contents of this file are subject to the Mozilla Public License
	Version 1.1 (the "License"); you may not use this file except in
	compliance with the License. You may obtain a copy of the License at
	http://www.mozilla.org/MPL/
	
	Software distributed under the License is distributed on an "AS IS"
	basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
	License for the specific language governing rights and limitations
	under the License.
	
	The Original Code is Fever Framework code.
	
	The Initial Developer of the Original Code is Romain Ecarnot.
	Portions created by Initial Developer are Copyright (C) 2006
	the Initial Developer. All Rights Reserved.
	
	Contributor(s):
	
	Unless required by applicable law or agreed to in writing, software
	distributed under the License is distributed on an "AS IS" BASIS,
	WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
	See the License for the specific language governing permissions and
	limitations under the License.
-----------------------------------------------------------------------------*/

import com.bourre.events.BubbleEventBroadcaster;
import com.bourre.events.EventType;
import com.bourre.events.IEvent;

import fever.exception.IllegalAccessException;
import fever.log.FeverDebug;
import fever.utils.GAnalytic;

import fvaswing.components.alert.FvAlertButton;
import fvaswing.components.FvAlert;
import fvaswing.components.reader.abstract.FvReader;
import fvaswing.components.reader.abstract.FvReaderEvent;
import fvaswing.components.reader.FvReaderResources;
import fvaswing.components.reader.playlist.FvPlayListItem;

/**
 * Abstract implementation for "PlayList reader document".
 *
 * @author Romain Ecarnot
 */
class fvaswing.components.reader.abstract.FvReaderDocument extends BubbleEventBroadcaster
{
	//-------------------------------------------------------------------------
	// Events definition
	//-------------------------------------------------------------------------
	
	/** Broadcasted when a new document start loading process. */
	public static var onNewDocumentEVENT : EventType = new EventType( "onNewDocument" );
	
	/** Broadcasted when document is fully loaded. */
	public static var onLoadDocumentEVENT : EventType = new EventType( "onLoadDocument" );
	
	/** Broadcasted during loading progress. */
	public static var onProgressDocumentEVENT : EventType = new EventType( "onProgressDocument" );
		
	/** Broadcasted when an error occured. */
	public static var onErrorDocumentEVENT : EventType = new EventType( "onErrorDocument" );
	
	
	//-------------------------------------------------------------------------
	// Private properties
	//-------------------------------------------------------------------------
	
	private var _alert : FvAlert;
	private var _pendingLoad : Boolean;
	private var _current : FvPlayListItem;
	
	
	//-------------------------------------------------------------------------
	// Public Properties
	//-------------------------------------------------------------------------
	
	/** Reader parent */
	public var parent : BubbleEventBroadcaster;
	
		
	//-------------------------------------------------------------------------
	// Public API
	//-------------------------------------------------------------------------
	
	/**
	 * Loads passed-in document {@link fvaswing.components.reader.playlist.FvPlayListItem}.
	 * 
	 * <p>If a loading process is is running, an 
	 * {@link fvaswing.components.FvAlert} dialog is open.<br />
	 * If passed-in {@code url} is same as current, do nothing.
	 * 
	 * @param url document url to load in reader. 
	 */
	public function load( item : FvPlayListItem ) : Void
	{
		if( _pendingLoad )
		{
			_openAlertDialog(
				FvReaderResources.getInstance().alertTitle,
				FvReaderResources.getInstance().alertAlreadyInUse
			);
		}
		else
		{
			if( item )
			{
				if( item.getURL() != _current.getURL() )
				{
					_current = item;
					_fireEventType( onNewDocumentEVENT );
					
					_load( item.getURL() );
				}
				else FeverDebug.DEBUG( "Document " + item.getURL() + " is already loaded" );
			}
		}
	}
	
	/**
	 * Resize document target.
	 * 
	 * <p>Must be override by concrete document subclass.
	 */
	public function setSize() : Void
	{
		throw new IllegalAccessException( "setSize() must be override" );
	}
	
	/**
	 * Returns current loaded {@link fvaswing.components.reader.playlist.FvPlayListItem}
	 */
	public function getDocument() : FvPlayListItem
	{
		return _current;
	}
	
	/**
	 * Returns current document url.
	 */
	public function getURL() : String
	{
		return _current.getURL();
	}
	
	/**
	 * Returns loaded percent of current document.
	 * 
	 * <p>Must be override by subclass ( otherwise an exception is thrown )
	 */
	public function getPercent() : Number
	{
		throw new IllegalAccessException( "getPercent() must be override" );
		return null;	
	}
	
 	/**
 	 * Releases document and all event listeners subscription.
 	 * 
 	 * <p>Must be override by subclass ( otherwise an exception is thrown ) 
 	 */
 	public function release() : Void
 	{
 		throw new IllegalAccessException( "release() must be override" );
 	}
 	
 	
	//-------------------------------------------------------------------------
	// Private implementation
	//-------------------------------------------------------------------------
	
	/**
	 * Constructor.
	 */
	private function FvReaderDocument( reader : FvReader )
	{
		super( this, reader );
		
		_pendingLoad = false;
	}
	
	/**
	 * Overrides for custom reader type.
	 */
	private function _getReader() : FvReader
	{
		return FvReader( parent );	
	}
	
	
	private function _openAlertDialog( title : String, message : String ) : Void
	{
		_createAlertDialog();
		
		_alert.title = title;
		_alert.message = message;
		_alert.defaultButton = FvAlertButton.OK;
		_alert.open();	
	}
	
	/**
	 * Overrides if you want to play with FvAlert style.
	 */
	private function _createAlertDialog() : Void
	{
		if( !_alert )
		{ 
			_alert = new FvAlert();
			_alert.enabledBlurEffect = false;
		}	
	}
	
	/**
	 * Overrides for cutom loading process
	 */
	private function _load( url : String ) : Void
	{
		throw new IllegalAccessException( "_load() must be override" );
	}
	
	private function _onDocumentLoaded( event : IEvent ) : Void
	{
		_pendingLoad = false;
		
		GAnalytic.trackOutBound( _current.getURL() );
	}
	
	private function _onDocumentProgress( event : IEvent ) : Void
	{
		_fireEventType( onProgressDocumentEVENT );
	}
	
	private function _onDocumentError( event : IEvent ) : Void
	{
		_current = null;
		_fireEventType( onErrorDocumentEVENT );
		
		_openAlertDialog( 
			FvReaderResources.getInstance().alertTitle,
			FvReaderResources.getInstance().alertError
		);
	}
	
	private function _fireCompleteEvent() : Void
	{
		_fireEventType( onLoadDocumentEVENT );	
	}
	
	private function _fireEventType( type : EventType ) : Void
	{
		broadcastEvent( new FvReaderEvent( type, _getReader() ) );	
	}
}